package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态
     *   参数：
     *      textContent     ：动态正文
     *      imageContent[]  : 文件数组
     */
    @PostMapping
    public ResponseEntity movements(String textContent, MultipartFile [] imageContent)
            throws IOException {
        movementsService.movements(textContent,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询个人动态
     *   请求连接：GET   /all
     *   请求参数：
     *     Query
     *       page，pagesize，userId
     */
    @GetMapping("/all")
    public ResponseEntity all(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,Long userId) {
        PageResult result = movementsService.findByUserId(page,pagesize,userId);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询好友动态
     */
    @GetMapping
    public ResponseEntity movements(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = movementsService.findByFriendId(page,pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = movementsService.findByRecommend(page,pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据id查询动态,进入手机首页的时候，报错
     *      GET  movements/{id}
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String id){
        MovementsVo vo = movementsService.findById(id);
        return ResponseEntity.ok(vo);
    }

    // /movements/visitors

    @Autowired
    private CommentsService commentsService;

    /**
     * 点赞功能
     *    GET  /movements/:id/like
     *    返回值
     */
    @GetMapping("/{movementId}/like")
    public ResponseEntity like(@PathVariable("movementId") String movementId) {
        //调用service完成点赞处理，返回点赞后的总数
        Integer likeCount = commentsService.like(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     *  GET  :id/dislike
     */
    @GetMapping("/{movementId}/dislike")
    public ResponseEntity dislike(@PathVariable("movementId") String movementId) {
        //调用service完成点赞处理，返回点赞后的总数
        Integer likeCount = commentsService.dislike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 查看首页的最新访客
     *    GET   /visitors
     *    返回值 ResponseEntity<List<TodayBest>>
     */
    //防止连接错误
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<TodayBest> vos = movementsService.visitors();
        return ResponseEntity.ok(vos);
    }
}